<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\ProjectTaskConfig as ProjectTaskConfigModel;
use app\Request;

class TaskConfig extends Base
{

    /**
     * 任务列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function taskList(Request $request)
    {
        try {
            $page = $request->param('page');
            $pagesize = $request->param('pagesize');
            $ProjectTaskConfigModel = new ProjectTaskConfigModel;
            $list = $ProjectTaskConfigModel->alias('p')
                ->field('*')
                ->paginate([
                    'list_rows'=> $pagesize,
                    'var_page' => 'page',
                    'page'=>$page
                ]);
            if($list){
                $result = ["status"=>true,"msg"=>"获取列表成功！","data"=>$list];
            }else{
                $result = ["status"=>false,"msg"=>"获取列表失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 任务添加
     * @param Request $request
     * @return \think\response\Json
     */
    public function addTask(Request $request){
        try {
            $data['task_name'] = $request->param('task_name');
            $data['task_tag'] = $request->param('task_tag');
            $data['mark'] = $request->param('mark');
            $data['create_time'] = time();

            $ProjectTaskConfigModel = new ProjectTaskConfigModel();
            $res = $ProjectTaskConfigModel->insertGetId($data);
            if($res){
                $result = ["status"=>true,"msg"=>"添加成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"添加失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 编辑任务
     * @param Request $request
     * @return \think\response\Json
     */
    public function saveTask(Request $request){
        try {
            $id = $request->param('id');
            if($request->param('task_name'))  $data['task_name'] = $request->param('task_name');
            if($request->param('task_tag'))  $data['task_tag'] = $request->param('task_tag');
            if($request->param('mark'))  $data['task_tag'] = $request->param('mark');
            $data['update_time'] = time();

            $ProjectTaskConfigModel = new ProjectTaskConfigModel();
            $res = $ProjectTaskConfigModel->where('id','=',$id)->save($data);

            if($res !== false){
                $result = ["status"=>true,"msg"=>"编辑成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"编辑失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }

    /**
     * 删除任务
     * @param Request $request
     * @return \think\response\Json
     */
    public function delTask(Request $request){
        try {

            $id = $request->param('id');
            $ProjectTaskConfigModel = new ProjectTaskConfigModel();
            $res = $ProjectTaskConfigModel->where('id','=',$id)->delete();
            if($res){
                $result = ["status"=>true,"msg"=>"删除成功！"];
            }else{
                $result = ["status"=>false,"msg"=>"删除失败！"];
            }
            return json($result);

        } catch (\Exception $e) {
            $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
            return json($error);
        }
    }
}
